package com.zjy.software.day12;

import java.util.ArrayList;
import java.util.Collection;

public class CollectionTest {
    public static void main(String[] args) {
        Collection collection = new ArrayList();

        // 判断集合是否为空
        System.out.println("collection.isEmpty() = " + collection.isEmpty());

        collection.add("hello");
        collection.add("world");
        // 集合的长度
        System.out.println("collection.size() = " + collection.size());

        //判断是否包含某个元素
        System.out.println("collection.contains(\"hello\") = " + collection.contains("hello"));

        collection.add(new Person("zhangsan"));
        System.out.println("collection.contains(new Person(\"zhangsan\")) = " + collection.contains(new Person("zhangsan")));

        //打印一个集合的时候默认打印全部元素,而不是具体的地址
        System.out.println("collection = " + collection);

        //删除集合中的元素
        collection.remove("hello");
        System.out.println("after remove collection = " + collection);


//        collection.clear();
//        System.out.println("after clear collection = " + collection);

        Object[] array = collection.toArray();
        System.out.println("array = " + array);

        for (Object object : collection) {
            System.out.println("object = " + object);
        }
    }
}
class Person{
    private String name;
    Person(String name){
        this.name=name;
    }
 public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Person)) return false;
        Person person = (Person) o;
        return name != null ? name.equals(person.name) : person.name == null;
    }
@Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }
 }
